package com.digital.welcome.controller;


import com.alibaba.fastjson.JSON;
import com.digital.welcome.annotation.OperationLog;
import com.digital.welcome.entity.User;
import com.digital.welcome.pojo.MyRequestBody;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.PermissionService;
import com.digital.welcome.service.RolePermissionService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 韩
 */
@RestController
@RequestMapping("/role-permission")
public class RolePermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation("角色权限查询")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public String selectUser(@RequestBody User user) {
        List<String> allPermissionByUsername = permissionService.selectAllPermissionNameByUsername(user.getUserName());
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(allPermissionByUsername));

    }

    @OperationLog()
    @ApiOperation("权限修改")
    @RequiresPermissions("Administration")
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public String setPermission(@RequestBody MyRequestBody<Integer, List<String>> requestBody) {
        Integer roleId = requestBody.getFirstParameter();
        List<String> list = requestBody.getSecondParameter();
        return rolePermissionService.setPermissionByRoleId(roleId, list);
    }

}


